/*
 * Created by SharpDevelop.
 * Author: Joey
 * Date: 26/09/2007
 * Time: 21:44
 * 
 * Source is copyright Joey Sabey 2007
 */

using System;
using System.IO;
using System.Windows.Forms;

namespace MonoTheism
{
	/// <summary>
	/// Description of Output.
	/// </summary>
	public static class Output
	{
		public static void Save(TreeNodeCollection tnc)
		{
			SaveFileDialog save = new SaveFileDialog();
			
			if(save.ShowDialog() == DialogResult.OK)
			{
				TrueSave(tnc, save.FileName);
			}
		}
		public static void Save(TreeNodeCollection tnc,
		                        string sName)
		{
			if(Directory.GetParent(sName).Exists)
			{
				TrueSave(tnc, sName);
			}
			else
				throw new Exception("Path invalid");
		}
		private static void TrueSave(TreeNodeCollection tnc,
		                             string sName)
		{
			PRAY_XML pxmlFile = new PRAY_XML(sName, tnc);
			pxmlFile.Save();
		}
		
		
		
		public static void Export(TreeNodeCollection tnc)
		{
			SaveFileDialog save = new SaveFileDialog();
			
			if(save.ShowDialog() == DialogResult.OK)
			{
				TrueExport(tnc, save.FileName);
			}
		}
		
		public static void Export(TreeNodeCollection tnc,
		                          string s)
		{
			if(Directory.GetParent(s).Exists)
			{
				TrueExport(tnc, s);
			}
			else
				throw new Exception("Path invalid");
		}
		
		private static void TrueExport(TreeNodeCollection tnc,
		                               string sFileName)
		{
			foreach(TreeNode node in tnc)
			{
				Block tag = (Block)node.Tag;
				if(tag.Type()=="FILE")
				{
					FILE_Block fbBuffer = (FILE_Block)tag;
					Program.pyFile.Add(fbBuffer.Source,
				               		   fbBuffer.EmbedName);
				}
				else if(tag.Type()=="AGNT")
				{
					AGNT_Block abBuffer = (AGNT_Block)tag;
					Program.pyFile.Add("AGNT",
					                   abBuffer.Name,
					                   abBuffer.Strings,
					                   abBuffer.Ints);
				}
				else
					throw new Exception("Unknown block type: "+tag.Type());
			}
			Program.pyFile.Save(sFileName);
		}
		
		private static void TrueExport(Block_List bl,
		                               string sFileName)
		{
			foreach(Block b in bl)
			{
				if(b.Type()=="FILE")
				{
					FILE_Block fbBuffer = (FILE_Block)b;
					Program.pyFile.Add(fbBuffer.Source,
				               		   fbBuffer.EmbedName);
				}
				else if(b.Type()=="AGNT")
				{
					AGNT_Block abBuffer = (AGNT_Block)b;
					Program.pyFile.Add("AGNT",
					                   abBuffer.Name,
					                   abBuffer.Strings,
					                   abBuffer.Ints);
				}
				else
					throw new Exception("Unknown block type: "+b.Type());
			}
			Program.pyFile.Save(sFileName);
		}
		
		public static void AutoBuild(String_List sl)
		{
			foreach(string s in sl)
			{
				TrueAutoBuild(s);
			}
		}
		public static void AutoBuild(string s)
		{
			
		}
		private static void TrueAutoBuild(string s)
		{
			//create file streams
			System.IO.TextWriter fsLong = null;
			System.IO.TextWriter fsShort = null;
			PRAY_XML pxmlFile = null;
			Options.Load();
			
			//open file streams
			fsShort = new StreamWriter("Logs/lastbuild.txt", false);
			if(Options.LongTermLog)
				fsLong = new StreamWriter("Logs/longtermbuild.txt", true);
			
			//AutoBuild
			WriteLine(fsShort, fsLong, "Attempting build: \'" + s + "\'...");
			try
			{
				pxmlFile = new PRAY_XML(s);
				WriteLine(fsShort, fsLong,
					      "Block count: " + pxmlFile.Load().Count.ToString());
				foreach(string path in Options.AutoBuild)
				{
					WriteLine(fsShort, fsLong,
							  "Building to: " +
							  path +
							  Path.DirectorySeparatorChar +
							  pxmlFile.FileName);
					TrueExport(pxmlFile.Load(),
					           path + 
					           Path.DirectorySeparatorChar +
					           pxmlFile.FileName);
				}
			}
			catch(FileNotFoundException)
			{
				WriteLine(fsShort, fsLong,
				          "PRAY XML file could not be found.");
			}
			catch(Exception e)
			{
				WriteLine(fsShort,
				          fsLong,
				          "Exception occured during build: " + e.ToString());
			}
			finally
			{
				WriteLine(fsShort, fsLong,
				          "Ending build..." +
				          '\r' + '\n');
			}
			
			//Close open files
			fsShort.Close();
			if(Options.LongTermLog)
				fsLong.Close();
		}
		private static void WriteLine(TextWriter twShort,
		                              TextWriter twLong,
		                              string s)
		{
			twShort.WriteLine(s);
			if(twLong != null)
				twLong.WriteLine(s);
		}
	}
}
